package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.InetAddress;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.RemoteUser;
import model.UserList;
/**
 * this class show the connected users list in a window
 * link to a chat gui and a connected user list
 */
public class UserListWindow extends JFrame implements ActionListener{
	private JList userJlist;
	private ChatGUI chatGUI;
	private DefaultListModel userList;
	
	public UserListWindow(DefaultListModel userList, ChatGUI chatGUI) {
		this.userList = userList;
		this.userJlist = new JList(userList);
		this.chatGUI = chatGUI;
		initComponents();	
	}
	
	/**
	 * initialize the connect window
	 */
	public void initComponents() {
		this.setTitle("User List Window");
		JPanel panel = new JPanel();
		JButton bDisconnect = new JButton("Disconnect");
		bDisconnect.addActionListener(this);
		userJlist.setForeground(Color.RED);
		userJlist.setBackground(Color.WHITE);
		userJlist.setSelectionForeground(Color.BLUE);
		userJlist.setSelectionBackground(Color.LIGHT_GRAY);
		userJlist.setFixedCellWidth(200);
		userJlist.setFixedCellHeight(20);
		userJlist.setFont(new Font("Arial",Font.ROMAN_BASELINE,13));
		userJlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        panel.add(userJlist);
        add(panel,BorderLayout.WEST);
		JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(this.userJlist);
        
        //create a mouseListener
        MouseListener mouseListener = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int index = userJlist.locationToIndex(e.getPoint());
                    if (index != -1){
	                    RemoteUser user = (RemoteUser) (userList.getElementAt(index));
	    				chatGUI.creerConversation(user);
    				}	
                 }
            }
        };
        
        userJlist.addMouseListener(mouseListener);
        this.add("South",bDisconnect);
        this.add(scrollPane);
		this.setSize(350, 500);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);	
    }
	
	/**
	 * get a RemoteUser in the list connected user when we know his IP address
	 * @param ip Address IP of remote user
	 * @return the remote user object
	 */
	public RemoteUser getUser(InetAddress ip){
		for (int i=0; i<userJlist.getModel().getSize(); i++){
			if (((RemoteUser) userJlist.getModel().getElementAt(i)).getIP().equals(ip)) {
				return ((RemoteUser) userJlist.getModel().getElementAt(i));
			}
		}		
		return null;
	}

	
	/**
	 * methods for ActionListener
	 * action when we click the disconnect button
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		for (int i=0; i<this.chatGUI.getListConversation().size();i++){
			this.chatGUI.getListConversation().get(i).dispose();
		}
		
		for (int i=0; i<this.chatGUI.getListConversation().size();i++){
			this.chatGUI.removeElementListConversation(this.chatGUI.getListConversation().get(i));
		}

		this.chatGUI.disconnect();
		this.dispose();
	}
}
